<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>受控组件</title>
</head>
<body>
  <!-- 准备容器 -->
  <div id="test"></div>
  <!-- 按照顺序引入 -->
  <script type="text/javascript" src="../../js/react.development.js"></script>
  <script type="text/javascript" src="../../js/react-dom.development.js"></script>
  <script type="text/javascript" src="../../js/babel.min.js"></script>
  <script type="text/babel" >
    class Login extends React.Component {
        // 初始化状态
        state ={username:'',password:''}
        // 修改数据到状态中
        editUsername=(e)=>{
            this.setState({username :e.target.value})
            console.log(e.target.value);
        }
        editPassword=(e)=>{
            this.setState({password :e.target.value})
            console.log(e.target.value);
        }
        handler=(event)=>{
            event.preventDefault()  // 阻止表单提交
            const {username,password} = this.state
            alert(`用户名是：${username},密码是：${password}`)
        }
        render(){
            // 受控组件  随着输入的内容  直接存到状态中  等需要的时候直接取出来  类似vue的双向数据绑定
            return (
                <form action="http://www.baidu.com" onSubmit={this.handler}>
                    用户名：<input onChange={this.editUsername} type="text" />
                    密码：<input onChange={this.editPassword}  type="password" />
                    <button>登录</button>
                </form>
            )
        }
    }
    ReactDOM.render(<Login/>,document.getElementById('test'))
  </script>
</body>
</html>